home *** CD-ROM | disk | FTP | other *** search
/ Almathera Ten Pack 3: CDPD 3 / Almathera Ten on Ten - Disc 3: CDPD3.iso / scope / 001-025 / scopedisk11 / rex / rex.c < prev    next >
C/C++ Source or Header  |  1995-03-18  |  5KB  |  201 lines

  1.  
  2. /*
  3.  *  REX.C
  4.  *
  5.  *      (c) Copyright 1987, 1988 by Kim DeVaughn, All Rights Reserved
  6.  *
  7.  *      This program is freely redistributable for non-commercial purposes.
  8.  *
  9.  *      For commercial use, you may contact the author via USENET at:
  10.  *          kim@amdahl.amdahl.com
  11.  *
  12.  *
  13.  *  Example of ARexx interface code, etc.
  14.  *
  15.  */
  16.  
  17. #include <stdio.h>
  18. #include "defs.h"
  19. #include "rexx.h"
  20.  
  21.  
  22. APTR OpenLibrary();
  23. APTR FindPort();
  24. APTR GetMsg();
  25. APTR CreateRexxMsg();
  26. APTR CreateArgstring();
  27.  
  28.  
  29. /*
  30. struct RexxArg *cmdarg;
  31.  
  32. struct MsgPort  RexxPort;
  33. struct MsgPort *ARexxPort;
  34.  
  35. struct RexxMsg *cmdptr;
  36. struct RexxMsg *retptr;
  37. */
  38.  
  39.  
  40. struct RxsLib  *RexxSysBase;
  41.  
  42.  
  43.  
  44. /*
  45.  *  issue a command to ARexx ...
  46.  */
  47.  
  48. do_rexx(cmdstr)
  49. char *cmdstr;
  50. {
  51.     struct RexxArg *cmdarg;
  52.  
  53.     struct MsgPort  RexxPort;
  54.     struct MsgPort *ARexxPort;
  55.  
  56.     struct RexxMsg *cmdptr;
  57.     struct RexxMsg *retptr;
  58.  
  59.     char host[16];
  60.     char hexbuf[12];      /* should only need 9 bytes */
  61.  
  62.     int ret, testcmd, i;
  63.  
  64.  
  65.     ClearMem(&RexxPort, sizeof(struct MsgPort));
  66.     strcpy(host, "DME");
  67.     sprintf(hexbuf, "%08x", &RexxPort);
  68.     strcat(host, hexbuf);
  69.     InitPort(&RexxPort, host);      /* need to error check this */
  70.     AddPort(&RexxPort);
  71.     /* return here if InitPort failed */
  72.  
  73.  
  74.     if (cmdarg = (struct RexxArg *)CreateArgstring(cmdstr, strlen(cmdstr))) {
  75.         if (cmdptr = (struct RexxMsg *)CreateRexxMsg(&RexxPort, "dme", host)) {
  76.             ACTION(cmdptr) = RXCOMM;
  77.             ARG0(cmdptr)   = (STRPTR)cmdarg;
  78.  
  79.             Forbid();
  80.             if (ARexxPort = (struct MsgPort *)FindPort("REXX")) {
  81.                 PutMsg(ARexxPort, cmdptr);
  82.                 Permit();
  83.  
  84.                 i = 0;
  85.                 for (;;) {
  86.                     WaitPort(&RexxPort);
  87.                     retptr = (struct RexxMsg *)GetMsg(&RexxPort);
  88.  
  89.                     printf("\nMessage = %d   Host = %s\n", i+1, host);
  90.                     if (IsRexxMsg(retptr)) printf("IsRexxMsg() says it be a REXX msg!\n");
  91.                     printf("rm_Args[0] = %s\n", ARG0(retptr));
  92.                     printf("rm_Result1 = %d\nrm_Result2 = %d\n", RESULT1(retptr), RESULT2(retptr));
  93.                     printf("rm_Action  = %08x\n", ACTION(retptr));
  94.                     printf("ln_Name    = %s\n", retptr->rm_Node.mn_Node.ln_Name);
  95.  
  96.                     if (IsRexxMsg(retptr)) {
  97.  
  98.                         /* here's where you process the command the ARexx macro issued */
  99.                         testcmd = strcmp(ARG0(retptr), "SECOND");
  100.                         if (testcmd == 0) do_rexx("second");
  101.                         testcmd = strcmp(ARG0(retptr), "CAT");
  102.                         if (testcmd == 0) do_rexx("cat");
  103.  
  104.                         RESULT1(retptr) = i;
  105.                         RESULT2(retptr) = 0;
  106.                         i++;
  107.  
  108.                         ReplyMsg(retptr);
  109.                     }
  110.                     if (cmdptr == retptr) break;
  111.                 }
  112.                 if (ret = RESULT1(retptr)) {
  113.                     if (RESULT2(retptr)) {
  114.                         if (RESULT2(retptr) == 1) {
  115.                             printf("Unknown Command\n");
  116.                         } else {
  117.                             printf("ARexx macro error:  Code = %d   Severity = %d\n", RESULT2(retptr), ret);
  118.                         }
  119.                     } else {
  120.                         printf("User specified macro error.  RC = %d\n", ret);
  121.                     }
  122.                 } else {
  123.                     printf("Normal macro termination.\n");
  124.                 }
  125.             } else {
  126.                 printf("ARexx not available - REXX port not found\n");
  127.                 ret = -1;
  128.             }
  129.             DeleteRexxMsg(cmdptr);
  130.         } else {
  131.             printf("CreateRexxMsg() Failed\n");     /* this may be overkill, and not need to be checked */
  132.             ret = -1;
  133.         }
  134.         DeleteArgstring(cmdarg);
  135.     } else {
  136.         printf("CreateArgstring() Failed\n");       /* this may be overkill, and not need to be checked */
  137.         ret = -1;
  138.     }
  139.     RemPort(&RexxPort);
  140.     FreePort(&RexxPort);
  141.     return(ret);
  142. }
  143.  
  144.  
  145.  
  146. /*
  147.  * initializion of our port to talk to ARexx
  148.  */
  149.  
  150. void
  151. rexxinit()
  152. {
  153.     char hexbuf[12];  /* should only need 9 bytes */
  154.  
  155.     /* need to add error checking */
  156.  
  157. /*  ClearMem(&RexxPort, sizeof(struct MsgPort));
  158.     strcpy(host, "DME");
  159.     sprintf(hexbuf, "%08x", &RexxPort);
  160.     strcat(host, hexbuf);
  161.     printf("\n\nhost = %s\n", host);
  162.  
  163.     InitPort(&RexxPort, host);
  164.     AddPort(&RexxPort);
  165. */  return();
  166. }
  167.  
  168.  
  169.  
  170. /*
  171.  * cleanup of our ARexx port
  172.  */
  173.  
  174. void
  175. rexxclose()
  176. {
  177. /*  RemPort(&RexxPort);
  178.     FreePort(&RexxPort);
  179. */  return();
  180. }
  181.  
  182.  
  183.  
  184. /* ......... test prog ........... */
  185.  
  186. main (argc, argv)
  187. int   argc;
  188. char *argv[];
  189. {
  190.     if (argc < 2) exit(5);
  191.  
  192.     if (RexxSysBase = (struct RxsLib *)OpenLibrary("rexxsyslib.library", (ULONG)RXSVERS)) {
  193.         /* rexxinit();  */
  194.         do_rexx(argv[1]);
  195.         /* rexxclose(); */
  196.         CloseLibrary(RexxSysBase);
  197.     }
  198.     exit(0);
  199. }
  200.  
  201.